package com.controller;

import com.github.pagehelper.PageInfo;
import com.pojo.*;
import com.service.*;
import com.vo.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.List;

@Controller
@RequestMapping("/property")
public class PropertyController {
    @Resource
    private PlotService plotService;

    @Resource
    private FloormanagerService floormanagerService;

    @Resource
    private HouseService houseService;

    @Resource
    private ZuHouseService zuHouseService;
    @Resource
    private OwnersService ownersService;
    @Resource
    private  SubletService subletService;

    @Resource
    private StorestateService storestateService;

    @RequestMapping("/plot/index")
    public String toplot(){
        return "/property/plot";
    }

    @RequestMapping("/plot/show")
    @ResponseBody
    public Result selectplot(Plot plot){
        System.out.println(plot);
        List<Plot> plots = plotService.selectallPlot(plot);
        return Result.success(plots);
    }
    @RequestMapping("/plot/add")
    @ResponseBody
    public Result addplot(Plot plot){
        plotService.insert(plot);
        return Result.success("添加成功");
    }
    @RequestMapping("/plot/delete")
    @ResponseBody
    public Result deleteplot(Integer id){
        int selectfloorbyplotid = floormanagerService.selectfloorbyplotid(id);
        if(selectfloorbyplotid>0){
            return Result.success("该小区还有楼栋");
        }else{
            plotService.deleteByPrimaryKey(id);
            return Result.success("删除成功");
        }

    }
    @RequestMapping("/plot/update")
    @ResponseBody
    public Result updateplot(Plot plot){
        System.out.println(plot.getId());
        int i = plotService.updateByPrimaryKey(plot);
        System.out.println(i);
        return Result.success("修改成功");
    }

    @RequestMapping("/floor/index")
    public String tofloor(){
        return "/property/floormanager";
    }

    @RequestMapping("/floor/show")
    @ResponseBody
    public Result selectfloor(FloorManagerVo floorManagerVo){
        System.out.println(floorManagerVo);
        PageInfo<Floormanager> pageInfo = floormanagerService.selectallFloor(floorManagerVo);
        return Result.success(pageInfo);
    }
    @RequestMapping("/floor/add")
    @ResponseBody
    public Result addfloor(Floormanager floormanager){
       floormanagerService.insertFloor(floormanager);
        return Result.success("添加成功");
    }
    @RequestMapping("/floor/delete")
    @ResponseBody
    public Result deletefloor(Integer id){
        int selectcountbyfloorid = houseService.selectcountbyfloorid(id);
        if(selectcountbyfloorid>0){
            return Result.success("该楼栋下还有住户");
        }else{
            floormanagerService.deleteFloor(id);
            return Result.success("删除成功");
        }

    }
    @RequestMapping("/floor/update")
    @ResponseBody
    public Result updatefloor(Floormanager floormanager){
       floormanagerService.updareFloor(floormanager);
        return Result.success("修改成功");
    }
    @RequestMapping("/house")
    public String house(){
        return "property/house";
    }

    @RequestMapping("/treelist")
    @ResponseBody
    public Result query(){
        List<TreeVo> list = plotService.queryTreeData();
        System.out.println(list);
        return Result.success(list);
    }
    @RequestMapping("/house/show")
    @ResponseBody
    public Result houseshow(HouseVo houseVo,  String plotname , Integer floorid){
        String ownername = houseVo.getOwnername();
        List<TreeVo> list = plotService.queryTreeData();
        Integer plotids = null;
        Integer flooids=floorid;
        for (TreeVo treeVo : list) {
            if(plotname.equals(treeVo.getTitle())){
                plotids=treeVo.getId();
                flooids=0;
                break;
            }else{
                List<TreeVo> children = treeVo.getChildren();
                for (TreeVo child : children) {
                    int selectplotidbyid = floormanagerService.selectplotidbyid(floorid);
                    plotids=selectplotidbyid;
                }
            }

        }
        houseVo.setPlotid(plotids);
        houseVo.setFloorid(flooids);
        PageInfo<House> housePageInfo = houseService.selectHouseall(houseVo);
        System.out.println(housePageInfo);
        return Result.success(housePageInfo);
    }

    @RequestMapping("/zucount")
    public String zucount(){
        return "property/zucount";
    }

    @RequestMapping("/zucount/show")
    @ResponseBody
    public Result selectzuByHouse(SubletVo subletVo){
        PageInfo<Sublet> selectsubletall = subletService.selectsubletall(subletVo);
        return Result.success(selectsubletall);
    }

    @RequestMapping("/zucount/update")
    @ResponseBody
    public Result updatestate(Sublet sublet){

        System.out.println(sublet);
        int updatestateByid = houseService.updatestateByids(2,sublet.getHouseid());
        if(updatestateByid>0){
            int i = subletService.insertSelective(sublet);
            if(i>0){
                return Result.success("出租成功");
            }else {
                return Result.success("出租失败");
            }
        }else {
            return Result.success("出租失败");
        }
      /*  Owners owners1 = ownersService.selectownerBycardno(zuHouseVo.getOwnercardno());
        if(owners1==null){
            Owners  owners=new Owners();
            owners.setOwnername(zuHouseVo.getOwnername());
            owners.setOwnercardno(zuHouseVo.getOwnercardno());
            owners.setOwnerphone(zuHouseVo.getOwnerphone());
            owners.setRoleid(5);
            owners.setPlotid(zuHouseVo.getPlotid());
            owners.setIsno(0);
            int i = ownersService.insertSelective(owners);
            Zuhouse zuhouse=new Zuhouse();
            zuhouse.setHouseid(zuHouseVo.getHouseid());
            zuhouse.setStarttime(zuHouseVo.getStarttime());
            zuhouse.setEndtime(zuHouseVo.getEndtime());
            if(i>0){
                int updatestateByid = houseService.updatestateByid(2, zuHouseVo.getHouseid(),owners.getId());
                zuHouseService.insertSelective(zuhouse);
            }
        }else {
            int i = ownersService.updateIsnoBycardno(owners1.getId(), 0);
            Zuhouse zuhouse=new Zuhouse();
            zuhouse.setHouseid(zuHouseVo.getHouseid());
            zuhouse.setStarttime(zuHouseVo.getStarttime());
            zuhouse.setEndtime(zuHouseVo.getEndtime());
            if(i>0){
                int updatestateByid = houseService.updatestateByid(2, zuHouseVo.getHouseid(),owners1.getId());
                zuHouseService.insertSelective(zuhouse);
            }
        }
        return Result.success("出租成功");*/

    }
    @RequestMapping("/shoucount/update")
    @ResponseBody
    public Result shouhouse(int id){
        int i = houseService.updatestateByids(1,id);
        int i1 = subletService.deleteByhouseid(id);
        return Result.success("成功收回");
    }


    @RequestMapping("/househetong")
    public  String househetong(HttpServletRequest request,Integer id){
        House house = houseService.selecthouseByid(id);
        System.out.println(house);
        Sublet sublet = subletService.selectByhouseid(house.getId());
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String starttime = sdf.format(sublet.getStarttime());
        String endtime = sdf.format(sublet.getEndtime());
        System.out.println(sublet);
        request.getSession().setAttribute("house",house);
        request.getSession().setAttribute("sublet",sublet);
        request.getSession().setAttribute("starttime",starttime);
        request.getSession().setAttribute("endtime",endtime);
        return "property/househetong";
    }


    @RequestMapping("/housestate")
    @ResponseBody
    public Result housestate(){
        List<Storestate> selectallhousestate = storestateService.selectallhousestate();
        return Result.success(selectallhousestate);
    }

}
